﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.ServiceProcess;


namespace IndexingService
{
    public partial class formIndexMain : Form
    {
        private Timer checkTimer = new Timer();
        private bool onBattery = true;
        private bool ManualOverride = false;

        private enum serviceAction
        {
            Start,
            Stop
        };

        public formIndexMain()
        {
            InitializeComponent();

            checkTimer.Tick += new EventHandler(timerCallback);
            checkTimer.Interval = 1000;
            checkTimer.Start();
            checkIndexingServiceStatus(this, null);
        }

        private void timerCallback(object sender, EventArgs e)
        {
            checkIndexingServiceStatus(sender, e);
            checkPowerLineStatus(sender, e);
        }

        private void checkIndexingServiceStatus(object sender, EventArgs e)
        {
            string ServiceName = "Windows Search";

            ServiceController sc = new ServiceController(ServiceName);
            if (sc.Status.Equals(ServiceControllerStatus.Running))
            {
                this.btnStart.Enabled = false;
                this.btnStop.Enabled = true;
            }
            else if (sc.Status == ServiceControllerStatus.Stopped)
            {
                this.btnStart.Enabled = true;
                this.btnStop.Enabled = false;
            }
            this.txtStatus.Text = this.txtNotifyStatus.Text = sc.Status.ToString();
        }

        private void checkPowerLineStatus(object sender, EventArgs e)
        {
            Type t = typeof(PowerStatus);
            PropertyInfo[] pi = t.GetProperties();
            PropertyInfo prop = null;
            for (int i = 0; i < pi.Length; i++)
            {
                if (pi[i].Name == "PowerLineStatus")
                {
                    prop = pi[i];
                }
            }
            object propVal = prop.GetValue(SystemInformation.PowerStatus, null);

            if (propVal.ToString() == "Online" && ManualOverride == false)
            {
                this.onBattery = true;
                serviceControl(serviceAction.Start);
            }
            else
            {
                this.onBattery = false;
                serviceControl(serviceAction.Stop);
            }

        }

        public bool getPowerStatus()
        {
            return this.onBattery;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            ManualOverride = true;
            serviceControl(serviceAction.Stop);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ManualOverride = true;
            serviceControl(serviceAction.Start);
        }

        private void serviceControl(serviceAction Action)
        {
            ServiceController sc = new ServiceController("Windows Search");
            switch (Action)
            {
                case serviceAction.Start:
                    if (sc.Status == ServiceControllerStatus.Stopped)
                        sc.Start();
                    break;
                case serviceAction.Stop:
                    if (sc.Status == ServiceControllerStatus.Running)
                        sc.Stop();
                    break;
            }
            checkIndexingServiceStatus(this, null);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void contextMenu_Opening(object sender, CancelEventArgs e)
        {

        }

        private void menuCloseExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuShowWindow_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Maximized;
        }


        /*
         * Window Drag implementation 
         */

        private bool dragStarted = false;
        

        private void dragPanel_MouseDown(object sender, MouseEventArgs e)
        {
            dragStarted = true;
        }

        private void dragPanel_MouseUp(object sender, MouseEventArgs e)
        {
            dragStarted = false;
        }

        private void dragPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragStarted == false && e.Button != MouseButtons.Left)
                return;

            // TODO: implement drag and drop

            MessageBox.Show(Location.X.ToString());
            MessageBox.Show(Location.Y.ToString());
            MessageBox.Show(e.X.ToString());
            MessageBox.Show(e.Y.ToString());
            
            int diffX = this.Location.X - e.X;
            int diffY = this.Location.Y - e.Y;

            /*this.Location = new Point(e.X-diffX, e.Y-diffY);*/

        }
    }
}
